//
//  NSDictionary+Common.m
//  TimeBoat
//
//  Created by Mac on 2020/3/11.
//  Copyright © 2020yang. All rights reserved.
//

#import "NSDictionary+Common.h"

@implementation NSDictionary (Common)


- (NSString *)hhe_convertToJsonData{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"%@",error);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

/**
 判断字典中某个key是否存在
 @param key 健
 @return YES/NO
 */
- (id)safeObjectForKey:(NSString*)key{
    id val = [self objectForKey:key];
    if ([val isEqual:[NSNull null]]) {
        return nil;
    }
    return val;
}

@end

#pragma mark - NSMutableDictionary
@implementation NSMutableDictionary (CommonMu)

#pragma mark - 字典安全处理
/**
 安全的对字典赋值
 @param anObject 值
 @param aKey 健
 */
- (void)safeSetObject:(id)anObject forKey:(id)aKey {
    if (anObject && aKey)
        [self setObject:anObject forKey:aKey];
}

@end
